﻿@using XCode;
@using XCode.Configuration;
@{
    var pair = Model as Pair;
    var entity = pair.First as IEntity;
    var item = pair.Second as FieldItem;
}
@if (item.IsIdentity)
{
    <td class="text-center hidden-md hidden-sm hidden-xs">@entity[item.Name]</td>
}
else
{
    switch (Type.GetTypeCode(item.Type))
    {
        case TypeCode.Boolean:
            if ((Boolean)entity[item.Name])
            {
                <td class="text-center hidden-md hidden-sm hidden-xs">
                    <i class="glyphicon glyphicon-ok" style="color: green;"></i>
                </td>
            }
            else
            {
                <td class="text-center hidden-md hidden-sm hidden-xs">
                    <i class="glyphicon glyphicon-remove" style="color: red;"></i>
                </td>
            }
            break;
        case TypeCode.DateTime:
            var dt = (DateTime)entity[item.Name];
            if (dt > DateTime.MinValue && dt.Year > 1900)
            {
                <td class="text-center hidden-md hidden-sm hidden-xs">@(((DateTime)entity[item.Name]).ToFullString(""))</td>
            }
            else
            {
                <td class="text-center hidden-md hidden-sm hidden-xs"></td>
            }
            break;
        case TypeCode.Decimal:
            <td class="text-center hidden-md hidden-sm hidden-xs">@entity[item.Name]</td>
            break;
        case TypeCode.Single:
        case TypeCode.Double:
            <td class="text-center hidden-md hidden-sm hidden-xs">@entity[item.Name]</td>
            break;
        case TypeCode.Byte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
            //特殊处理枚举
            if (item.Type.IsEnum)
            {
                var v = entity[item.Name];
                var val = v + "";
                try
                {
                    val = System.EnumHelper.GetDescription((Enum)v) ?? v + "";
                }
                catch { }
                <td class="text-center hidden-md hidden-sm hidden-xs">@val</td>
            }
            else
            {
                <td class="text-center hidden-md hidden-sm hidden-xs">@Convert.ToInt64(entity[item.Name]).ToString("n0")</td>
            }
            break;
        case TypeCode.String:
        default:
            {
                var val = entity[item.Name];
                var map = item.Map;
                if (map != null && map.Provider != null && val + "" != "")
                {
                    var dic = new RouteValueDictionary();
                    var key = map.Provider.Key;
                    if (!key.IsNullOrEmpty() && item.OriField != null) { dic[key] = entity[item.OriField.Name]; }
                    <td class="text-center hidden-md hidden-sm hidden-xs">@Html.ActionLink(entity[item.Name] + "", "Edit", map.Provider.EntityType.Name, dic, null)</td>
                }
                else
                {
                    <td class="text-center hidden-md hidden-sm hidden-xs">@entity[item.Name]</td>
                }
            }
            break;
            @*default:
                <td>@entity[item.Name]</td>
                break;*@
    }
}